<?php
	require_once('globals.php');
	$notlogin = true;
	require_once('inc/weixiconfig.php');

	$data=array('data'=>array(),'error'=>'');
    
    /*
    case "tmp_login":
		        $_SESSION[APPID.'_userinfo']['tbid'] = 1;
		    break;
    */
    
	if(!empty($_GET['ac'])){
		$ac=$_GET['ac'];
		switch($ac){
		    
		    /* case "tmp_login":
		        $_SESSION[APPID.'_userinfo']['tbid'] = 1;
		    break; */
		    
		    
			case "checkwclogin":
				$data['error']=checkwclogin(2,$_SERVER['HTTP_REFERER']);
				if($data['error']===true){
					$data['userid']=$db->get('tb_member', 'tbid', array('tbid' => $_SESSION[APPID.'_userinfo']['tbid']));
				}
			break;
			case "member/wechatlogin":
				if(isset($_POST['openid']) && isset($_POST['nickname']) && isset($_POST['ip'])){
					$member=$db->get('tb_member', '*', array('openid' => $_POST['openid']));
					if(!$member){
						$set = array(
							'nickname' => filterEmoji($_POST['nickname']),
							'openid' => $_POST['openid'],
							'#edittime' => 'NOW()',
							'#createtime' =>'NOW()'
						);
						$db->insert('tb_member', $set);
						$member = $db->get('tb_member', '*', array('openid' => $_POST['openid']));
						if(!empty($_POST['headimgurl'])){
							$path = 'dofiles/'.$member['tbid'].'/head/';
							$file= getweixincontents($_POST['headimgurl'],'','GET',3,1);
							$data['error']=uploadwechatimg($path,$file);
						}
						$code=shorturl($member['tbid']);
						//$db->update('tb_member', array('invitationcode'=>$code[0]), array('id' => $member['id']));
					}
					$_SESSION[APPID.'_userinfo']=$data['data']=$member;
					$db->insert('login_log',array('member_id'=>$_SESSION[APPID.'_userinfo']['tbid'],'ip'=>$_POST['ip'],'#dt'=>'now()'));
					$data['data']['session_id']=session_id();
				}else{
					$data['error'] = "数据缺失，请检查";
				}
			break;
			
			case "member/info":
				if(checkwclogin(1)){
					$member=$db->get('tb_member','*',array('tbid'=>$_SESSION[APPID.'_userinfo']['tbid']));
					if(file_exists('dofiles/'.$_SESSION[APPID.'_userinfo']['tbid'].'/head/head.jpg')){
						$src='dofiles/'.$_SESSION[APPID.'_userinfo']['tbid'].'/head/head.jpg?ran='.rand(10000,99999);
					}else{
						$src='static/img/head.png';
					}
					$member['head']=$src;
//                  $member['mobile']=$member['mobile'];
                    $where['member_id'] = $_SESSION[APPID.'_userinfo']['tbid'];
                    $where['type'] = 4;
                    $begintime=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y')));
                    $endtime=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1);
                    $where['creatdate[<>]'] = array($begintime,$endtime);
					$data['data']=$member;
				}else{
					$data['error'] = "登陆超时，请刷新页面，重新登录";
				}
			break;
			
			
			case "member/head":
				if(checkwclogin(1)){
					if(isset($_POST['localid'])){
						$file=mediaget($_POST['localid']);
						$path='dofiles/'.$_SESSION[APPID.'_userinfo']['tbid'].'/head/';
						$data['error']=uploadwechatimg($path,$file);
					}else{
						$data['error']='参数缺失';
					}
				}else{
					$data['error'] = "登陆超时，请刷新页面，重新登录";
				}
			break;
			
			
			case "mediaget":
				if(isset($_POST['localid'])){
					$data=mediaget($_POST['localid'],$_POST['file']);
				}else{
					$data['error'] = "数据缺失，请检查";
				}
			break;
			
			//问题列表
			case "question_list":
			    if(checkwclogin(1)){
			        $data['data'] = $db->select('tb_question_bank', '*');
			        
 			        /* foreach($data['data'] as $v){
			            $str .= '|'.$v['tbid'].','.$v['category_id'].','.'a';
			        }
			        
			        echo $str;exit; */
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			     
			    echo json_encode($data);exit;
			break;
			
			//set
			case "get_set":
			    if(checkwclogin(1)){
			        $data = $db->get('tb_setting', '*');
			         
			         
			        echo json_encode($data);exit;
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			    	
			    echo json_encode($data);exit;
			break;
			
			//专家列表
			case "expert_list":
			    if(checkwclogin(0)){
			        $data['data'] = $db->select('tb_class', '*', array('type'=>3, 'order'=>"index_id asc"));
			         
			        /* foreach($data['data'] as $v){
			         $str .= '|'.$v['tbid'].','.$v['category_id'].','.'a';
			         }
			          
			        echo $str;exit; */
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			
			    echo json_encode($data);exit;
			break;
			
			//answer_str = id,ca_id,a|
			case "answer":
			    if(checkwclogin(1)){
			        if(isset($_POST['answer_str'], $_POST['ch_sex'], $_POST['ch_age'], $_POST['p_name'], $_POST['secs'], $_POST['mark'])){
			            $score_arr = array('a'=>0, 'b'=>1, 'c'=>2, 'd'=>3);
			            $an_arr = explode("|", $_POST['answer_str']);
			            
			            
			            
			            $score_ca = array();
			            $an_str = "";
			            $ca_str = "";
			            
			            //校验个数
			            if(count($an_arr) != 26){
			                $data['error'] = "答案数目不对";
			                echo json_encode($data);exit;
			            }
			            
			            foreach($an_arr as $k=>$v){
			                $tmp_arr = explode(',', $v);

			                $an_str .= '|'.$tmp_arr[2];
			                
			                //计算分类得分
			                $score_ca[$tmp_arr[1]] = $score_ca[$tmp_arr[1]] ? $score_ca[$tmp_arr[1]] : 0;
			                $score_ca[$tmp_arr[1]] = $score_ca[$tmp_arr[1]] + $score_arr[$tmp_arr[2]];
			            }
			            
			            foreach($score_ca as $v){
			                $ca_str .= '|'.$v;
			            }
			            
			            $an_str = ltrim($an_str, '|');
			            $ca_str = ltrim($ca_str, '|');
			            
			            //$db->delete('tb_test_result', array('member_id'=>$_SESSION[APPID.'_userinfo']['tbid']));
			            $db->insert('tb_test_result', array('anser'=>$an_str, 'score'=>$ca_str, 'member_id'=>$_SESSION[APPID.'_userinfo']['tbid'], 'createtime'=>date("Y-m-d H:i:s"), 'edittime'=>date("Y-m-d H:i:s"), 'sec_spent'=>$_POST['secs'],'child_sex'=>$_POST['ch_sex'],'child_age'=>$_POST['ch_age'],'name'=>$_POST['p_name'],'mobile'=>$_POST['p_phone']));
			            
			            $set = array('child_sex'=>$_POST['ch_sex'], 'child_age'=>$_POST['ch_age'], 'realname'=>$_POST['p_name']);
			            
			            $mobile = $db->get('tb_member', 'mobile', array('tbid'=>$_SESSION[APPID.'_userinfo']['tbid']));
			            if(!$mobile){
			                $set['mobile'] = $_POST['p_phone'];
			                $ca_tbid = $db->get('tb_captcha', 'tbid', array('mobile'=>$_POST['p_phone'], 'captcha'=>$_POST['captcha'], 'status'=>0));
			                
			                if(!$ca_tbid){
			                    $data['error'] = "验证码错误";
			                    echo json_encode($data);exit;
			                }
			                
			                $db->update('tb_captcha', array('status'=>1), array('tbid'=>$ca_tbid));
			            }
			            $set['type'] = $_POST['mark'];
			            
			            $db->update('tb_member', $set, array('tbid'=>$_SESSION[APPID.'_userinfo']['tbid']));
			            
			            $data['issuccess'] = 1;
			        }else{
			            $data['error']='参数缺失';
			        }
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			    
			    echo json_encode($data);exit;
			break;
			
			
			//测试结果
			case "result":
			    if(checkwclogin(1)){
			        $mem_info = $db->get('tb_member', '*', array('tbid'=>$_SESSION[APPID.'_userinfo']['tbid']));
			        
			        $result = $db->get('tb_test_result', '*', array('member_id'=>$_SESSION[APPID.'_userinfo']['tbid'], 'is_del'=>0, 'order'=>'createtime desc'));
			        
			        $data['data']['ch_age'] = $mem_info['child_age'];
			        $data['data']['ch_sex'] = $mem_info['child_sex'];
			        $data['data']['test_time'] = $result['createtime'];
			        $data['data']['sec_spent'] = $result['sec_spent'];
			        
			        $data['data']['three'] = explode('|', $result['score']);
			        
			        $ca_id = $db->select('tb_question_category', 'tbid');
			        
			        $data['data']['test'] = array();
			        foreach($data['data']['three'] as $k=>&$v){
			            $sum = $db->count('tb_question_bank', array('category_id'=>$ca_id[$k]));
			            $v = round($v/$sum, 1);
			            
			            if(0 <= $v && $v < 1){
			                array_push($data['data']['test'], "正常");
			            }else if(1 <= $v && $v < 1.55){
			                array_push($data['data']['test'], "轻度");
			            }else if(1.55 <= $v && $v < 2){
			                array_push($data['data']['test'], "中度");
			            }else{
			                array_push($data['data']['test'], "严重");
			            }
			        }
			        
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			
			    echo json_encode($data);exit;
			break;
			
			
			//课程详情  type 1 公开课 2正式课 3专家
			
			case "class_info":
			    if(checkwclogin(1)){
			        if(isset($_GET['type'])){
			            $class_info = $db->get('tb_class', '*', array('type'=>$_GET['type'],'tbid'=>$_GET['tbid'],'order'=>'tbid desc'));
			            
			            $data['data'] = $class_info;
			            $data['data']['price'] = round($data['data']['price']/100, 2);
			            $data['data']['subsist'] = round($data['data']['subsist']/100, 2);
			        }
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
				
			    echo json_encode($data);exit;
			break;
			
			
			//公开正式课订单  type 1 公开课 2正式课
			case "gen_order":
			    if(checkwclogin(1)){
			        if(isset($_POST['type'], $_POST['ch_name'], $_POST['ch_sex'], $_POST['ch_age'], $_POST['p_phone'], $_POST['p_name'])){
			            $class_info = $db->get('tb_class', '*', array('type'=>$_POST['type'], 'order'=>'tbid desc'));
			            $or_info = $db->get('tb_order', '*', array('member_id'=>$_SESSION[APPID.'_userinfo']['tbid'],'class_id'=>$class_info['tbid'], 'status[>]'=>1));
			            if($or_info){
			                $data['error'] = "已付款";
			                echo json_encode($data);exit;
			                
			            }else{
			                $order_no = getordernum("OR");
			                
			                $db->update('tb_order', array('status'=>0), array('member_id'=>$_SESSION[APPID.'_userinfo']['tbid'],'class_id'=>$class_info['tbid']));
			                
			                $set = array('order_number'=>$order_no, 'member_id'=>$_SESSION[APPID.'_userinfo']['tbid'], 'subsist'=>$class_info['subsist'], 'full_payment'=>$class_info['price'], 'status'=>1, 'type'=>$_POST['type'], 'class_id'=>$class_info['tbid'], 'createtime'=>date("Y-m-d H:i:s"), 'edittime'=>date("Y-m-d H:i:s"));
			                
			                
			                $or_id = $db->insert('tb_order', $set);
			            }
			            
			            $data['order_no'] = $order_no;
			            
			            //删除老数据
			            $ht = $db->prepare("delete from tb_order_mem where order_id=?");
			            $ht->execute(array($or_id));
			            
			            //记录用户信息
			            $set = array('order_id'=>$or_id, 'ch_age'=>$_POST['ch_age'], 'ch_sex'=>$_POST['ch_sex'], 'ch_name'=>$_POST['ch_name'], 'p_phone'=>$_POST['p_phone'], 'p_name'=>$_POST['p_name']);
			            $db->insert('tb_order_mem', $set);
			        }else{
			            $data['error'] = "参数错误";
			        }
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			
			    echo json_encode($data);exit;
			break;
			
			
			//专家订单
			case "gen_order_pro":
		        if(checkwclogin(1)){
			        if(isset($_POST['type'], $_POST['p_phone'], $_POST['p_name'],$_POST['is_test'], $_POST['remark'], $_POST['num'], $_POST['class_id'], $_POST['reserve_time'], $_POST['mark'])){
			            $class_info = $db->get('tb_class', '*', array('type'=>$_POST['type'], 'tbid'=>$_POST['class_id']));
                        
		                $order_no = getordernum("OR");
		                
		                //$db->update('tb_order', array('status'=>0), array('member_id'=>$_SESSION[APPID.'_userinfo']['tbid'],'class_id'=>$class_info['tbid']));
		                
		                $set = array('order_number'=>$order_no, 'member_id'=>$_SESSION[APPID.'_userinfo']['tbid'], 'subsist'=>$class_info['subsist']*$_POST['num'], 'full_payment'=>$class_info['price'] * $_POST['num'], 'status'=>1, 'type'=>3, 'class_id'=>$class_info['tbid'], 'reserve_number'=>$_POST['num'], 'createtime'=>date("Y-m-d H:i:s"), 'edittime'=>date("Y-m-d H:i:s"));
		                
		                //print_r($_REQUEST);exit;
		                
		                if($_REQUEST['qrcode'] == 1) $set['source'] = 2;
		                
		                $or_id = $db->insert('tb_order', $set);
			            
			            $data['order_no'] = $order_no;
			            
			            //删除老数据
			            //$ht = $db->prepare("delete from tb_order_mem where order_id=?");
			            //$ht->execute(array($or_id));
			            
			            //删除老数据
			            //$ht = $db->prepare("delete from tb_expert_reserve where order_id=?");
			            //$ht->execute(array($or_id));
			             
			            //记录用户信息
			            $set = array('order_id'=>$or_id, 'p_phone'=>$_POST['p_phone'], 'p_name'=>$_POST['p_name']);
			            $db->insert('tb_order_mem', $set);
			            //记录预约信息
			            $set = array('order_id'=>$or_id, 'expert_id'=>1, 'reserve_time'=>$_POST['reserve_time'], 'createtime'=>date("Y-m-d H:i:s"), 'edittime'=>date("Y-m-d H:i:s"), 'is_test'=>$_POST['is_test'], 'remark'=>$_POST['remark']);
			            $db->insert('tb_expert_reserve', $set);
			            
			            
			            $mobile = $db->get('tb_member', 'mobile', array('tbid'=>$_SESSION[APPID.'_userinfo']['tbid']));
			            
			            if(!$mobile){
			                $ca_tbid = $db->get('tb_captcha', 'tbid', array('mobile'=>$_POST['p_phone'], 'captcha'=>$_POST['captcha'], 'status'=>0));
			                 
			                if(!$ca_tbid){
			                    $data['error'] = "验证码错误";
			                    echo json_encode($data);exit;
			                }
			                 
			                $db->update('tb_captcha', array('status'=>1), array('tbid'=>$ca_tbid));
			                
			                
			                $set = array('type'=>$_POST['mark']);
			                $set['mobile'] = $_POST['p_phone'];
			                $db->update('tb_member', $set, array('tbid'=>$_SESSION[APPID.'_userinfo']['tbid']));
			            }
			             
			            
			            
			            
			        }else{
			            $data['error'] = "参数错误";
			        }
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			    	
			    echo json_encode($data);exit;
			break;
			
			
			//关闭订单
			case "close_order":
			    if(checkwclogin(1)){
			        if(isset($_POST['order_id'])){
			            $status = $db->get('tb_order', 'status', array('tbid'=>$_POST['order_id'], 'member_id'=>$_SESSION[APPID.'_userinfo']['tbid']));
			            
			            if($status != 1){
			                $data['error'] = "关闭失败";
			                echo json_encode($data);exit;
			            }
			            
			            $db->update('tb_order', array('status'=>0), array('tbid'=>$_POST['order_id'], 'member_id'=>$_SESSION[APPID.'_userinfo']['tbid']));
			            $data['issuccess'] = 1;
			        }else{
			            $data['error'] = "参数不对";
			        }
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			
			    echo json_encode($data);exit;
			break;
			
			
			//订单列表
			case "order_list":
			    if(checkwclogin(1)){
			        $status = array(
			            '0' => '已关闭',
			            '1' => '未付款',
			            '2' => '预付款',
			            '3' => '已付全款',
			            '4' => '已完成',
			            '5' => '已完成'
			        ); 
			        
			        
			        $set = array(
			            'order'=>'createtime desc',
			            'member_id'=>$_SESSION[APPID.'_userinfo']['tbid']
			        );
			        //1,2,3,4
			        if(isset($_GET['type'])){
			            if($_GET['type'] == 1){
			                $set['status'] = 1;
			            }elseif($_GET['type'] == 2){
			                $set['status'] = array(2,3);
			            }elseif($_GET['type'] == 3){
			                $set['status'] = array(4,5);
			            }elseif($_GET['type'] == 4){
			                $set['status'] = 0;
			            }
			        }
			        
			        $data['data'] = $db->select('tb_order', '*', $set);
			         
			         
			        foreach($data['data'] as &$v){
			            $v['msg'] = $status[$v['status']];
			            $tmp = $db->get('tb_class', 'name,image,class_time', array('tbid'=>$v['class_id']));
			            
			            $v['class_name'] = $tmp['name'];
			            $v['class_time'] = $tmp['class_time'];
			            
			            $v['image'] = SITEROOTURL.$tmp['image'];
			        }
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			    	
			    echo json_encode($data);exit;
			break;
			
			//检测手机
			case "check_mobile":
			    if(checkwclogin(1)){
		            $mobile = $db->get('tb_member', 'mobile', array('tbid'=>$_SESSION[APPID.'_userinfo']['tbid']));
		            if($mobile){
		                $data['status'] = 2;
		                $data['error'] = "已验证";
		            }else{
		                $data['status'] = 1;
		                $data['error'] = "未验证";
		            }
			             
			             
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			    	
			    echo json_encode($data);exit;
			break;
			
			
			//发送短信
			case "mobile_send":
			    if(checkwclogin(1)){
			        if(isset($_POST['mobile'])){
			            $count = $db->count('tb_member', array('mobile'=>$_POST['mobile']));
			            if($count > 0){
			                $data['status'] = 2;
			                $data['error'] = "手机号已被使用";
			                echo json_encode($data);exit;
			            }
			            
			            
			            $cap=rand(100000, 999999);
			            $db->insert('tb_captcha', array('mobile' =>$_POST['mobile'],'captcha' => $cap,'type' => 1,'#creattime' => 'now()','#edittime' =>'DATE_ADD(now(), INTERVAL 30 MINUTE)'));
			            
			            $data_tmp = array('mobile'=>$_POST['mobile'], 'number'=>$cap);
			            $echo = send_captch($data_tmp);
			            
			            if ($echo->Code == 'OK') {
			                $data['status'] = 1;
			            } else {
			                $data['status'] = 3;
			                $data['error'] = $echo->Message;
			            }
			            
			        }else{
			            $data['error'] = "参数错误";
			        }
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			
			    echo json_encode($data);exit;
			break;
			
			
			//submit
			case "submit":
			    if(checkwclogin(1)){
			        if(isset($_POST['mobile'], $_POST['captcha'])){
			            
			            $capid = $db->get('tb_captcha','tbid',array('mobile' => $_POST['mobile'],'captcha' => $_POST['captcha'],'type' => 1,'status' => 0,'#creattime[<=]' =>'now()','#edittime[>=]' =>'now()'));
			            if(empty($capid)){
			                $data['status'] = 2;
			                $data['error'] = "验证码错误";
			                echo json_encode($data);exit;
			            }else{
			                $db->update('tb_captcha', array('status' => 1), array('tbid' => $capid));
			                
			                $db->update('tb_member', array('mobile'=>$_POST['mobile']), array('tbid'=>$_SESSION[APPID.'_userinfo']['tbid']));
			                
			                $data['status'] = 1;
			                echo json_encode($data);exit;
			            }
			            
			        }else{
			            $data['error'] = "参数错误";
			        }
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			    	
			    echo json_encode($data);exit;
			break;
			
			
			//订单详情
			case "order_info":
			    if(checkwclogin(1)){
			        if($_GET['order_id']){
    			        $status = array(
    			            '0' => '已关闭',
    			            '1' => '未付款',
    			            '2' => '预付款',
    			            '3' => '已付全款',
    			            '4' => '已完成',
    			            '5' => '已完成'
    			        );
    			         
    			        $data['data'] = $db->get('tb_order', '*', array('tbid'=>$_GET['order_id']));
    			         
    			        $order_info = $db->get('tb_order_mem', 'p_name, p_phone', array('order_id'=>$_GET['order_id']));
    			        $data['data']['mobile'] = $order_info['p_phone'];
    			        $data['data']['realname'] = $order_info['p_name'];
    			        
    			        $data['data']['remark'] = $db->get('tb_expert_reserve', 'remark', array('order_id'=>$_GET['order_id']));
    			        
    			        $tmp = $db->get('tb_class', 'class_time,name', array('tbid'=>$data['data']['class_id']));
    			        
    			        $data['data']['class_time'] = $tmp['class_time'];
    			        $data['data']['class_name'] = $tmp['name'];
    			        $data['data']['subsist'] = round($data['data']['subsist']/100,2);
    			        
    			        if($data['data']['status'] > 1) $data['data']['pre_pay'] = '已付款'; else $data['data']['pre_pay'] = '未付款';
    			        if($data['data']['status'] > 2) $data['data']['all_pay'] = '已付款'; else $data['data']['all_pay'] = '未付款';
			        }else{
			            $data['error'] = "参数不对";
			        }
			    }else{
			        $data['error'] = "登陆超时，请刷新页面，重新登录";
			    }
			    	
			    echo json_encode($data);exit;
			    break;
			
			    
			    case "trade_pay":
			        if(checkwclogin(1)){
			            if(!empty($_POST['ordernumber'])){
			                $ordernumber=$_POST['ordernumber'];
			                if(1){
			                    try{
			                        $db->beginTransaction();
			                        $tradenumbers=array();
			                        $totals=0;
			                        $title='title';

		                            $orderinfo=$db->get('tb_order','*',array('order_number'=>$ordernumber));
		                            if(!$orderinfo){
		                                $data['error'] = '订单不存在';
		                                echo json_encode($data);exit;
		                            }
		                            if($orderinfo['status'] != 1){
		                                $data['error'] = '已支付的订单';
		                                echo json_encode($data);exit;
		                            }

			                        $data=array_merge($data,unifiedorder(array('title'=>$title,'tradenumber'=>$orderinfo['order_number'],'total'=>round($orderinfo['subsist']/100, 2))));
			                        $db->commit();
			                    }catch(Exception $e){
			                        $db->catche($e);
			                        exit;
			                    }
			                    $data['tradenumber']=$orderinfo['order_number'];
			                    //                            dump($data);exit;
			                }else{
			                    $data['error'] = "错误的订单记录";
			                }
			            }else{
			                $data['error'] = "数据缺失，请检查";
			            }
			        }else{
			            $data['error'] = "登陆超时，请刷新页面，重新登录";
			        }
			        echo json_encode($data);exit;
			    break;
			    
			    
			
			//获取点赞列表
			case 'getZanList':
				if(checkwclogin(1) == true){
					if(isset($_GET['from']) && isset($_GET['to'])){
						$where['vote_endtime[>]'] = date('Y-m-d 0:00:00', strtotime('-7 day'));
						$count = $db->count('tb_zan', $where);
						$data['count'] = $count;
						$where['order'] = array('createtime'=>'DESC');
						$where['limit'] = array($_GET['from'], $_GET['to']);
						$data['data'] = $db->select('tb_zan', '*', $where);
					}else{
						$data['error'] = '参数缺失';
					}
				}else{
					$data['error'] = 'login';
				}
				break;
			//获取点赞详情
			
				
			case 'getZanInfo':
				if(isset($_GET['tbid'])){
					if(checkwclogin(1) == true){
						$where['AND']['tbid'] = $_GET['tbid'];
						$db->update('tb_zan', array('viewcount[+]' => 1), $where);
						$zan = $db->get('tb_zan', '*', $where);
						if($zan){
							$data['data'] = $zan;
							$data['data']['joincount'] = $db->count('tb_zan_member', array('zan_id' => $_GET['tbid']));
							$data['data']['votecount']=floatval($db->get('tb_zan_member','sum(votenumber)',array('zan_id' => $_GET['tbid'])));
						}else{
							$data['error'] = '数据不存在';
						}
					}else{
						$data['error'] = 'login';
					}
				}else{
					$data['error'] = '参数缺失';
				}
				break;
			//获取点赞详情投票列表
			case 'getZanMember':
				if(isset($_GET['from']) && isset($_GET['to']) && isset($_GET['tbid'])){
					if(isset($_GET['name'])){
						$where['and']['or']['name[~]'] = $_GET['name'];
						$where['and']['or']['mobile'] = $_GET['name'];
					}
					$where['and']['zan_id'] = $_GET['tbid'];
					$where['and']['isshow'] = 1;
					$count = $db->count('tb_zan_member', $where);
					$data['count'] = $count;
					$where['limit'] = array($_GET['from'], $_GET['to']);
					$data['data'] = $db->select('tb_zan_member', '*', $where);
				}else{
					$data['error'] = '参数缺失';
				}
				break;
			//报名参赛
			//POST
			case 'joinZan':
				if(checkwclogin(1) == true){
					if(isset($_POST['zan_id']) && isset($_POST['name']) && isset($_POST['info']) && isset($_POST['mobile']) && isset($_POST['idcard']) && isset($_POST['province']) && isset($_POST['city']) && isset($_POST['area']) && isset($_POST['address']) && isset($_POST['title']) && isset($_POST['img']) && isset($_POST['img1']) && isset($_POST['img2'])){
						$where['and']['zan_id'] = $_POST['zan_id'];
						$where['and']['member_id'] = $_SESSION[APPID.'_userinfo']['tbid'];
						$where['and']['isshow[!]'] = 0;
						if(!$db->has('tb_zan_member', $where)){

							$db->insert('tb_zan_member', array(
								'zan_id' => $_POST['zan_id'],
								'member_id' => $_SESSION[APPID.'_userinfo']['tbid'],
								'name' => $_POST['name'],
								'mobile' => $_POST['mobile'],
								'idcard' => $_POST['idcard'],
								'province' => $_POST['province'],
								'city' => $_POST['city'],
								'area' => $_POST['area'],
								'address' => $_POST['address'],
								'title' => $_POST['title'],
								'info' => $_POST['info'],
								'img' => $_POST['img'],
								'img1' => $_POST['img1'],
								'img2' => $_POST['img2'],
								'#createtime' => 'NOW()'
							));
						}else{
							$data['error'] = '已经报名过了，请勿重复报名';
						}
					}else{
						$data['error'] = '参数缺失';
					}
				}else{
					$data['error'] = 'login';
				}
				break;
			//点赞投票
			//POST
			case 'vote':
				if(isset($_POST['zan_member_id']) && isset($_POST['zan_id'])){
					$zan = $db->get('tb_zan', '*', array('tbid' => $_POST['zan_id']));
					$todaycount = $db->count('tb_vote', array(
						'AND' => array(
							'member_id' => $_SESSION[APPID.'_userinfo']['tbid'],
							'zan_id' => $_POST['zan_id'],
							'createtime[>=]' => date('Y-m-d 0:00:00'),
							'createtime[<]' => date('Y-m-d 0:00:00', strtotime('+1 day'))
						)
					));
					if($todaycount < $zan['everydayvotecount']){
						$allcount = $db->count('tb_vote', array(
							'AND' => array(
								'member_id' => $_SESSION[APPID.'_userinfo']['tbid'],
								'zan_id' => $_POST['zan_id']
							)
						));
						if($allcount < $zan['allvotecount']){

							if($zan['isrepeatvote'] == 1){
								$db->insert('tb_vote', array(
									'member_id' => $_SESSION[APPID.'_userinfo']['tbid'],
									'zan_member_id' => $_POST['zan_member_id'],
									'zan_id' => $_POST['zan_id'],
									'#createtime' => 'NOW()'
								));
								$db->update('tb_zan_member', array('votenumber[+]' => 1), array('tbid' => $_POST['zan_member_id']));

							}else{
								if(!$db->has('tb_vote', array(
									'AND' => array(
										'member_id' => $_SESSION[APPID.'_userinfo']['tbid'],
										'zan_member_id' => $_POST['zan_member_id'],
										'zan_id' => $_POST['zan_id'],
										'createtime[>=]' => date('Y-m-d 0:00:00'),
										'createtime[<]' => date('Y-m-d 0:00:00', strtotime('+1 day'))
									)
								))){
									$db->insert('tb_vote', array(
										'member_id' => $_SESSION[APPID.'_userinfo']['tbid'],
										'zan_member_id' => $_POST['zan_member_id'],
										'zan_id' => $_POST['zan_id'],
										'#createtime' => 'NOW()'
									));
									$db->update('tb_zan_member', array('votenumber[+]' => 1), array('tbid' => $_POST['zan_member_id']));
								}else{
									$data['error'] = '已经投票过了，请勿重复投票';
								}
							}
						}else{
							$data['error'] = '总投票数已达上限';
						}
					}else{
						$data['error'] = '今日投票数已达上限';
					}
				}else{
					$data['error'] = '参数缺失';
				}
				break;
		}
	}
	echo json_encode($data);
?>
